package cn.bytecollege.admin.dao;

import cn.bytecollege.common.dao.BaseDao;
import cn.bytecollege.common.entity.Category;
import cn.bytecollege.common.entity.Cuisine;

import java.util.List;

public class CategoryDao extends BaseDao<Category,Integer> {
    /**
     * 查询到所有的分离在修改页面显示
     * @authon 11819
     */

    public List<Category> find() {
        String sql="select category_id,category_name from category";
        return helper.executeQuery(sql,Category.class);
    }
    /**
     * 查询所有分类
     * @param objects
     * @return
     */
    @Override
    public List<Category> find(Object... objects) {
        String sql="select category_id,category_name from category";
        return  helper.executeQuery(sql,Category.class);
    }

    /**
     *根据ID删除分类
     * @param categoryId
     * @return
     */
    @Override

    public int deleteById(Integer categoryId) {
        String sql="delete from category where category_id=?";
        return  helper.executeUpdate(sql,categoryId);
    }

    /**
     * 找到分类ID（再去修改）
     * @param categoryId
     * @return
     */
    @Override
    public Category findById(Integer categoryId) {
        String sql="select category_id,category_name from category where category_id= ? ";
        return helper.executeQuery(sql,Category.class,categoryId).get(0);
    }


    /**
     * 根据Id修改分类名称
     * @param category
     * @return
     */
    @Override
    public int update(Category category) {
        String sql="update category set category_name=? where category_id=?";
        Object[] objects={category.getCategoryName(),category.getCategoryId()};
        return helper.executeUpdate(sql,objects);
    }

    /**
     * 查询分类名称是否重复
     * @param categoryName
     * @return
     */
    public Category findByCategoryName(String categoryName){
        String sql="select category_name from category where category_name=?";
        List<Category> list=helper.executeQuery(sql,Category.class,categoryName);
        Category category=null;
        if(list.size()>0){
            category=list.get(0);
        }
        return category;
    }
    /**
     * 在分类管理中添加分类
     * @param category
     * @return
     */
    @Override
    public int save(Category category) {
        String sql = "INSERT INTO CATEGORY(CATEGORY_NAME) VALUES(?)";
        return helper.executeUpdate(sql,category.getCategoryName());
    }

    /**
     * 根据分类名称查找是否已经存在该分类
     * @param categoryName
     * @return
     */
    public Category findCategoryByCategoryName(String categoryName){
        String sql = "SELECT CATEGORY_ID,CATEGORY_NAME FROM CATEGORY WHERE CATEGORY_NAME = ?";
        List<Category> list = helper.executeQuery(sql,Category.class,categoryName);

        //判断列表中是否有该分类，如果有则list中至少有一条数据
        Category category = null;
        if(list.size()>0){
            category = list.get(0);
        }
        return category;
    }
}
